package com.zw.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zw.common.constant.WareConstant;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.common.utils.PageUtils;
import com.zw.common.utils.Query;

import com.zw.gulimall.ware.dao.PurchaseDetailDao;
import com.zw.gulimall.ware.entity.PurchaseDetailEntity;
import com.zw.gulimall.ware.service.PurchaseDetailService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        /**
         *    key: '华为',//检索关键字
         *    status: 0,//状态
         *    wareId: 1,//仓库id
         */
        LambdaQueryWrapper<PurchaseDetailEntity> wrapper = new LambdaQueryWrapper<>();
        String key = params.get("key").toString();
        if (StringUtils.isNotEmpty(key)) {
            wrapper.and(w -> {
                w.like(PurchaseDetailEntity::getPurchaseId, key)
                        .or().like(PurchaseDetailEntity::getSkuId, key);
            });
        }
        String status = params.get("status").toString();
        String wareId = params.get("wareId").toString();
        if (StringUtils.isNotEmpty(status)) {
            wrapper.eq(PurchaseDetailEntity::getStatus, status);
        }

        if (StringUtils.isNotEmpty(wareId)) {
            wrapper.eq(PurchaseDetailEntity::getWareId, wareId);
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void updatePurchaseDetailStatusByPurchaseIds(List<Long> ids) {
        LambdaQueryWrapper<PurchaseDetailEntity> wrapper = new LambdaQueryWrapper<PurchaseDetailEntity>()
                .in(PurchaseDetailEntity::getPurchaseId, ids);
        List<PurchaseDetailEntity> entities = baseMapper.selectList(wrapper);
        List<PurchaseDetailEntity> collected = entities.stream().map(item -> {
            item.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
            return item;
        }).collect(Collectors.toList());
        this.saveOrUpdateBatch(collected);
    }

    @Override
    public Boolean checkPurchaseStatus(List<PurchaseDetailEntity> list) {
        List<Long> ids = list.stream().map(item -> item.getId()).collect(Collectors.toList());
        LambdaQueryWrapper<PurchaseDetailEntity> wra = new LambdaQueryWrapper<PurchaseDetailEntity>()
                .in(PurchaseDetailEntity::getId, ids);
        List<PurchaseDetailEntity> entities = baseMapper.selectList(wra);
        Boolean flag = true;
        for (PurchaseDetailEntity x : entities) {
            if (x.getStatus() == WareConstant.PurchaseDetailStatusEnum.BUYING.getCode()
                    || x.getStatus() == WareConstant.PurchaseDetailStatusEnum.FINISH.getCode()
            || x.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()) {
                flag = false;
                break;
            }
        }
        return flag;
    }

}
